package com.woniuxy.joylifemarket.service.impl;

import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.entity.ShopAddress;
import com.woniuxy.common.entity.ShopUserAddress;
import com.woniuxy.common.entity.ShopUserAddressExample;
import com.woniuxy.common.mapper.CenterUserMapper;
import com.woniuxy.common.mapper.ShopAddressMapper;
import com.woniuxy.common.mapper.ShopUserAddressMapper;
import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.joylifemarket.service.ShopAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopAddressServiceImpl implements ShopAddressService {

    @Autowired
    ShopUserAddressMapper shopUserAddressMapper;
    @Autowired
    ShopAddressMapper shopAddressMapper;
    @Autowired
    CenterUserMapper centerUserMapper;

    @Override
    public List<ShopUserAddress> findAllAddressByUserId(String userId) {
        //找到地址映射对象
        ShopUserAddressExample shopUserAddressExample = new ShopUserAddressExample();
        ShopUserAddressExample.Criteria criteria = shopUserAddressExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<ShopUserAddress> shopUserAddresses = shopUserAddressMapper.selectByExample(shopUserAddressExample);
        for (ShopUserAddress shopUserAddress:shopUserAddresses) {
            ShopAddress shopAddress = shopAddressMapper.selectByPrimaryKey(shopUserAddress.getAddressId());
            CenterUser user = centerUserMapper.selectByPrimaryKey(shopUserAddress.getUserId());
            shopUserAddress.setShopAddress(shopAddress);
            shopUserAddress.setUser(user);
        }
        return shopUserAddresses;
    }

    @Override
    public ShopUserAddress findDefaultAddressByUserId(String userId) {
        ShopUserAddressExample shopUserAddressExample = new ShopUserAddressExample();
        ShopUserAddressExample.Criteria criteria = shopUserAddressExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andPriorityEqualTo("默认");
        List<ShopUserAddress> shopUserAddresses = shopUserAddressMapper.selectByExample(shopUserAddressExample);
        for (ShopUserAddress shopUserAddress:shopUserAddresses) {
            ShopAddress shopAddress = shopAddressMapper.selectByPrimaryKey(shopUserAddress.getAddressId());
            CenterUser user = centerUserMapper.selectByPrimaryKey(shopUserAddress.getUserId());
            shopUserAddress.setShopAddress(shopAddress);
            shopUserAddress.setUser(user);
        }
        ShopUserAddress shopUserAddress = shopUserAddresses.get(0);
        return shopUserAddress;
    }

    @Override
    public int addAddressByInfo(ShopAddress shopAddress, ShopUserAddress shopUserAddress) {
        //新建地址表
        shopAddress.setAddrId(KeyUtil.generateKey());
        int insert = shopAddressMapper.insert(shopAddress);
        if (insert!=0) {
            //新建地址表成功后，新建映射表
            shopUserAddress.setId(KeyUtil.generateKey());
            shopUserAddress.setAddressId(shopAddress.getAddrId());
            int insert1 = shopUserAddressMapper.insert(shopUserAddress);
            if (insert1!=0) {
                return  1;
            }
        }
        return 0;
    }
}
